package com.uduew.sjd.ui.repayment

import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.api.RepaymentScheduleApi
import com.uduew.sjd.bean.HttpBean
import com.uduew.sjd.config.Const
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.RepaymentScheduleRequester
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.utils.Utils
import com.uduew.sjd.view.xpop.RepaymentDetailsXpop
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.uduew.sjd.api.AllLoanOrdersApi
import com.uduew.sjd.utils.SpannableUtils

class RepaymentDetailFragment : BaseFragment() {

    lateinit var mState: RepaymentDetailStates
    private lateinit var mMessenger: PageMessenger
    private lateinit var mRepaymentScheduleRequester: RepaymentScheduleRequester
    var isInit: Boolean = true
    var orderDetail: AllLoanOrdersApi.ResAllLoanOrdersBean.MamaguyBean.ThoroughBean? = null

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(RepaymentDetailStates::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)
        mRepaymentScheduleRequester =
            getFragmentScopeViewModel(RepaymentScheduleRequester::class.java)//还款计划
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        orderDetail = arguments?.getParcelable("orderDetail")
        orderDetail?.let {
            it.thalassochemical.let {
                if (it != null) {
                    val date = Utils.timestampToDate(it.toLong())
                    mState.thalassochemical.set(date)
                }
            }
            mState.nuciformStr.set(it.nuciformStr)
            mState.phylogenic.set(it.phylogenic)
            mState.nuciform.set(it.nuciform)
        }

        mRepaymentScheduleRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<RepaymentScheduleApi.ResRepaymentScheduleBean>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.data.let {
                it?.tubulure.let { it1 ->
                    if (it1 != null) {
                        mState.tubulure.set(
                            Const.CURRENCY_SYMBOL + SpannableUtils.getDoubleFormat(
                                it1
                            )
                        )
                    }
                }
                if (!isInit) {
                    showDetail(
                        it?.telegraphoscope.toString(),
                        it?.herzegovina.toString(),
                        it?.strabismal.toString(),
                        "",
                        "",
                        it?.tubulure.toString(),
                        ""
                    )
                }
            }
        }
        WaitDialog.show(getString(R.string.loading))
        mRepaymentScheduleRequester.request(this@RepaymentDetailFragment, orderDetail?.beastly)
    }


    fun showDetail(
        amountReceived: String,
        interestInterest: String,
        serviceFee: String,
        taxesFees: String,
        channelMount: String,
        totalAmount: String,
        creditService: String
    ) {
        var pop = RepaymentDetailsXpop(
            requireActivity(),
            amountReceived,
            interestInterest,
            serviceFee,
            taxesFees,
            channelMount,
            totalAmount,
            creditService
        )
        pop.setOnAgreeClickListener {
        }
        XPopup.Builder(requireContext())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .asCustom(pop)
            .show()
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.fragment_repayment_detail, BR.vm, mState).addBindingParam(
            BR.click,
            Click()
        )
    }


    inner class Click {
        fun navBack() {
            if (!Utils.checkDebounce()) return
            nav().navigateUp()
        }

        fun nowRepayment() {
            if (!Utils.checkDebounce()) return
            WaitDialog.show(getString(R.string.loading))
            isInit = false
            val bundle = Bundle().apply {
                putString("orderId", orderDetail?.beastly)
                putString("calculator", mState.tubulure.get())
            }
            nav().navigate(R.id.action_navigation_repayment_to_extension_type_fragment, bundle)
        }

        fun rollOverRepayment() {
//            if (!Utils.checkDebounce()) return
//            val bundle = Bundle().apply {
//                putParcelable("orderDetail", orderDetail)
//            }
//            nav().navigate(R.id.action_navigation_repayment_to_extension_detail_fragment, bundle)
        }
    }

    class RepaymentDetailStates : StateHolder() {
        val tubulure: State<String> = State("")
        val thalassochemical: State<String> = State("")
        val nuciformStr: State<String> = State("")
        val nuciform: State<Double> = State(0.0)
        val phylogenic: State<String> = State("")
    }

}